package reflect;

import com.sun.org.apache.bcel.internal.classfile.Code;
import com.sun.org.apache.bcel.internal.classfile.LineNumberTable;
import com.sun.org.apache.bcel.internal.classfile.LocalVariableTable;

import java.lang.reflect.Method;

/**
 * Created by zhj on 15-10-20.
 */
public class Test1 {
    public static void main(String[] args) {
        try {
            Class clazz=Class.forName("reflect.Test1$Test2");
            Method method=clazz.getMethod("addA",int.class);

            Object obj=clazz.newInstance();
            method.invoke(obj,3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static class Test2{
        public String addA(int a){
            return "b";
        }
    }









    /*

    main方法字节码查看    javap -c -s -l -verbose Test1.class
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
    stack=6, locals=4, args_size=1
            0: ldc           #2                  // String reflect.Test2
            2: invokestatic  #3                  // Method java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
            5: astore_1
    6: aload_1
    7: ldc           #4                  // String addA
            9: iconst_1
    10: anewarray     #5                  // class java/lang/Class
            13: dup
    14: iconst_0
    15: getstatic     #6                  // Field java/lang/Integer.TYPE:Ljava/lang/Class;
            18: aastore
    19: invokevirtual #7                  // Method java/lang/Class.getMethod:(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
            22: astore_2
    23: aload_1
    24: invokevirtual #8                  // Method java/lang/Class.newInstance:()Ljava/lang/Object;
            27: astore_3
    28: aload_2
    29: aload_3
    30: iconst_1
    31: anewarray     #9                  // class java/lang/Object
            34: dup
    35: iconst_0
    36: iconst_3
    37: invokestatic  #10                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
            40: aastore
    41: invokevirtual #11                 // Method java/lang/reflect/Method.invoke:(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
            44: pop
    45: goto          53
            48: astore_1
    49: aload_1
    50: invokevirtual #13                 // Method java/lang/Exception.printStackTrace:()V
            53: return
    Exception table:
    from    to  target type
    0    45    48   Class java/lang/Exception
    LineNumberTable:
    line 11: 0
    line 12: 6
    line 14: 23
    line 15: 28
    line 18: 45
    line 16: 48
    line 17: 49
    line 19: 53
    LocalVariableTable:
    Start  Length  Slot  Name   Signature
    6      39     1 clazz   Ljava/lang/Class;
    23      22     2 method   Ljava/lang/reflect/Method;
    28      17     3   obj   Ljava/lang/Object;
    49       4     1     e   Ljava/lang/Exception;
    0      54     0  args   [Ljava/lang/String;
    StackMapTable: number_of_entries = 2
    frame_type = 112 *//* same_locals_1_stack_item *//*
    stack = [ class java/lang/Exception ]
    frame_type = 4 *//* same *//*
*/
}
